package com.chenglu.android.couponcenter.mvp.presenter;


import com.chenglu.android.common.base.BasePresenter;
import com.chenglu.android.common.net.Constant;
import com.chenglu.android.common.net.RxScheduler;
import com.chenglu.android.common.net.exception.NetErrorException;
import com.chenglu.android.common.utils.LogUtils;
import com.chenglu.android.common.utils.ToastUtils;
import com.chenglu.android.couponcenter.mvp.contract.CouponCenterContract;
import com.chenglu.android.couponcenter.mvp.model.CouponCenterModel;
import com.chenglu.android.goodsinfo.bean.ShopCouponPage;
import com.chenglu.android.index.bean.TabTitleBean;
import com.chenglu.android.mine.bean.CommonDataBean;
import com.chenglu.android.mine.bean.CouponBean;

import java.util.Map;

import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;


/**
 * @author: lyj
 * @Created by Copyright 2023/3/2 Guangzhou Hongma Technology Co., Ltd. All rights reserved.
 */
public class CouponCenterPresenter extends BasePresenter<CouponCenterContract.View> implements CouponCenterContract.CouponCenterPresenter {

    private CouponCenterContract.CouponCenterModel model;

    public CouponCenterPresenter() {
        model = new CouponCenterModel();
    }


    @Override
    public void getCouponList(String token, Map<String,String> map) {
        if(!isViewAttached()){
            return;
        }
        model.getCouponList(token,map)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new Observer<CouponBean>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mView.showLoading();
                    }

                    @Override
                    public void onNext(@NonNull CouponBean bean) {
                        mView.dismissLoading();
                        if(bean.getCode()==Constant.HTTPCODE200){
                            mView.getCouponListSuccess(bean);
                        }else {
                            ToastUtils.show(bean.getMsg());
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mView.dismissLoading();
                        NetErrorException.apiError(e,mView);

                        LogUtils.e("qqqqqqq:"+"onError:"+e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });
    }

    @Override
    public void getShopCouponPage(String token, Map<String, String> map) {
        if(!isViewAttached()){
            return;
        }
        model.getShopCouponPage(token,map)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new Observer<ShopCouponPage>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mView.showLoading();
                    }

                    @Override
                    public void onNext(@NonNull ShopCouponPage bean) {
                        mView.dismissLoading();
                        if(bean.getCode()==Constant.HTTPCODE200){
                            mView.getShopCouponPage(bean);
                        }else {
                            ToastUtils.show(bean.getMsg());
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mView.dismissLoading();
                        NetErrorException.apiError(e,mView);

                        LogUtils.e("qqqqqqq:"+"onError:"+e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });
    }

    @Override
    public void getShopCouponreceive(String token, String id) {
        if(!isViewAttached()){
            return;
        }
        model.getShopCouponreceive(token,id)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new Observer<CommonDataBean>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mView.showLoading();
                    }

                    @Override
                    public void onNext(@NonNull CommonDataBean bean) {
                        mView.dismissLoading();
                        if(bean.getCode()==Constant.HTTPCODE200){
                            mView.getShopCouponreceive(bean);
                        }else {
                            ToastUtils.show(bean.getMsg());
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mView.dismissLoading();
                        NetErrorException.apiError(e,mView);

                        LogUtils.e("qqqqqqq:"+"onError:"+e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });
    }

    @Override
    public void getTabTitle() {
        if(!isViewAttached()){
            return;
        }
        model.getTabTitle()
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new Observer<TabTitleBean>() {
                    //订阅
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mView.showLoading();

                    }

                    @Override
                    public void onNext(@NonNull TabTitleBean bean) {


//                        mView.getUserInfoSuccess(bean);
                        if(bean.getCode()==(Constant.HTTPCODE200)){
                            mView.getTabTitle(bean);
                        }else {
                            mView.getTabTitle(bean);
                        }

                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mView.dismissLoading();
                        NetErrorException.apiError(e,mView);
                        //记得上线删除
//                        ToastUtils.show(e.getMessage());
                        LogUtils.e("qqqqqqq:"+"onError:"+e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });
    }
}
